<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="javascript,Array,数组,prototype," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="对数组产生影响方法栈方法pushpush()方法可以接收任意数量的参数，把它们逐个添加到数组末尾，并返回修改后数组的长度。 poppop()方法会删除数组最后一项，并将该项返回。 队列方法shiftshift()方法会删除数组的第一项，并将该项返回。 unshiftunshift()方法可以在数组前端添加任意个项，并返回修改后数组的长度。 重排序方法reversereverse()方法可以反转数组">
<meta name="keywords" content="javascript,Array,数组,prototype">
<meta property="og:type" content="article">
<meta property="og:title" content="JS中数组的常用方法">
<meta property="og:url" content="http://yoursite.com/2016/12/22/JS数组常用方法/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="对数组产生影响方法栈方法pushpush()方法可以接收任意数量的参数，把它们逐个添加到数组末尾，并返回修改后数组的长度。 poppop()方法会删除数组最后一项，并将该项返回。 队列方法shiftshift()方法会删除数组的第一项，并将该项返回。 unshiftunshift()方法可以在数组前端添加任意个项，并返回修改后数组的长度。 重排序方法reversereverse()方法可以反转数组">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-09T10:03:25.724Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JS中数组的常用方法">
<meta name="twitter:description" content="对数组产生影响方法栈方法pushpush()方法可以接收任意数量的参数，把它们逐个添加到数组末尾，并返回修改后数组的长度。 poppop()方法会删除数组最后一项，并将该项返回。 队列方法shiftshift()方法会删除数组的第一项，并将该项返回。 unshiftunshift()方法可以在数组前端添加任意个项，并返回修改后数组的长度。 重排序方法reversereverse()方法可以反转数组">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/12/22/JS数组常用方法/"/>





  <title>JS中数组的常用方法 | Hexo</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/12/22/JS数组常用方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="shine-wl">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://dynamic-image.yesky.com/300x-/uploadImages/upload/20140912/upload/201409/wylnwjynkxijpg.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JS中数组的常用方法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-12-22T11:37:16+08:00">
                2016-12-22
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="对数组产生影响方法"><a href="#对数组产生影响方法" class="headerlink" title="对数组产生影响方法"></a>对数组产生影响方法</h2><h3 id="栈方法"><a href="#栈方法" class="headerlink" title="栈方法"></a>栈方法</h3><h4 id="push"><a href="#push" class="headerlink" title="push"></a>push</h4><p>push()方法可以接收任意数量的参数，把它们逐个添加到数组末尾，并返回修改后数组的长度。</p>
<h4 id="pop"><a href="#pop" class="headerlink" title="pop"></a>pop</h4><p>pop()方法会删除数组最后一项，并将该项返回。</p>
<h3 id="队列方法"><a href="#队列方法" class="headerlink" title="队列方法"></a>队列方法</h3><h4 id="shift"><a href="#shift" class="headerlink" title="shift"></a>shift</h4><p>shift()方法会删除数组的第一项，并将该项返回。</p>
<h4 id="unshift"><a href="#unshift" class="headerlink" title="unshift"></a>unshift</h4><p>unshift()方法可以在数组前端添加任意个项，并返回修改后数组的长度。</p>
<h3 id="重排序方法"><a href="#重排序方法" class="headerlink" title="重排序方法"></a>重排序方法</h3><h4 id="reverse"><a href="#reverse" class="headerlink" title="reverse"></a>reverse</h4><p>reverse()方法可以反转数组项的顺序。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">var arr = [&apos;one&apos;, &apos;two&apos;, &apos;three&apos;];</div><div class="line">arr.reverse(); </div><div class="line">console.log(arr); // [&apos;three&apos;, &apos;two&apos;, &apos;one&apos;]</div></pre></td></tr></table></figure>
<h4 id="sort"><a href="#sort" class="headerlink" title="sort"></a>sort</h4><p>sort()方法会对数组进行排序。<br>为了实现排序，sort()方法会调用每个数组项的toString()转型方法，然后比较得到的<strong>字符串</strong>。<br>sort()方法可以接收一个比较函数作为参数。该比较函数接收两个参数，如果第一个参数应该位于第二个参数之前，则返回一个负数；反之返回正数；若两个参数相等则返回0.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">// 默认排序</div><div class="line">var arr = [&apos;B&apos;, &apos;C&apos;, &apos;A&apos;];</div><div class="line">arr.sort();</div><div class="line">console.log(arr); // [&apos;A&apos;, &apos;B&apos;, &apos;C&apos;]</div><div class="line"></div><div class="line">// 接收比较函数</div><div class="line">var values = [5, 1, 10, 0];</div><div class="line">values.sort(function(a, b) &#123;</div><div class="line">    return a-b;  // 升序</div><div class="line">    // return b-a;  // 降序</div><div class="line">&#125;);</div><div class="line">console.log(values); // [0,1,5,10]</div></pre></td></tr></table></figure>
<h3 id="操作方法"><a href="#操作方法" class="headerlink" title="操作方法"></a>操作方法</h3><h4 id="splice"><a href="#splice" class="headerlink" title="splice"></a>splice</h4><p>splice()方法可以从指定的索引开始删除若干元素，然后再从该位置添加若干元素。<br>splice()方法始终都会返回一个空数组，该数组中包含从原始数组中删除的项。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">var arr = [&apos;Microsoft&apos;, &apos;Apple&apos;, &apos;Yahoo&apos;, &apos;AOL&apos;, &apos;Excite&apos;, &apos;Oracle&apos;];</div><div class="line">var arrDel = arr.splice(2, 3, &apos;Google&apos;, &apos;Facebook&apos;); // 从索引2开始删除3个元素,然后再添加两个元素</div><div class="line">console.log(arr);  // [&apos;Microsoft&apos;, &apos;Apple&apos;, &apos;Google&apos;, &apos;Facebook&apos;, &apos;Oracle&apos;]</div><div class="line">console.log(arrDel);  // [&apos;Yahoo&apos;, &apos;AOL&apos;, &apos;Excite&apos;]</div></pre></td></tr></table></figure>
<h1 id="所有方法总结"><a href="#所有方法总结" class="headerlink" title="所有方法总结"></a>所有方法总结</h1><h1 id="push-pop-unshift-shift"><a href="#push-pop-unshift-shift" class="headerlink" title="push() pop() unshift() shift()"></a>push() pop() unshift() shift()</h1><p>把这些方法看成栈操作：前两者正常的栈操作，后两者是反向的栈操作<br>push()和unshift()往数组中从后面、前面添加元素，并返回新数组的长度<br>pop()和shift()删除数组中最后、最前的元素，并返回删除的元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [];</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.push(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>));<span class="comment">//3</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">//[1, 2, 3]</span></div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.pop());<span class="comment">// 3</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">//[1,2]</span></div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.push([<span class="number">4</span>,<span class="number">5</span>]));<span class="comment">//3</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">// [1, 2, [4, 5]]</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [];</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.unshift(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>));<span class="comment">//3</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">//[1, 2, 3]</span></div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.shift());<span class="comment">// 1</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">// [2, 3]</span></div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(arr.unshift([<span class="number">4</span>,<span class="number">5</span>]));<span class="comment">//3</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">//[[4, 5], 2, 3]</span></div></pre></td></tr></table></figure>
<a id="more"></a>
<h1 id="join"><a href="#join" class="headerlink" title="join()"></a>join()</h1><p>Array.join()方法将数组中所有元素都转化为字符串并连接在一起，返回最后生成的字符串。可以自己指定分隔的符号，如果不指定，默认使用逗号</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</div><div class="line"><span class="built_in">console</span>.log(arr.join());<span class="comment">//"1,2,3"</span></div><div class="line"><span class="built_in">console</span>.log(arr.join(<span class="string">"-"</span>));<span class="comment">//"1-2-3"</span></div><div class="line"> </div><div class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">10</span>); <span class="comment">//长度为10的空数组 组成下边字符串</span></div><div class="line"><span class="built_in">console</span>.log(a.join(<span class="string">"-"</span>));<span class="comment">//"---------"</span></div></pre></td></tr></table></figure>
<h1 id="reverse-1"><a href="#reverse-1" class="headerlink" title="reverse()"></a>reverse()</h1><p>Array.reverse()方法将数组中的元素颠倒顺序，返回逆序的数组（返回的数组是其本身，原始数组已经改变了）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</div><div class="line">arr.reverse();</div><div class="line"><span class="built_in">console</span>.log(arr.join());<span class="comment">//"3,2,1"</span></div></pre></td></tr></table></figure>
<p>所以，如果想把一个字符串逆序的话，可以这样</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"abcdefg"</span>;</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(str.split(<span class="string">""</span>).reverse().join(<span class="string">""</span>));<span class="comment">//"gfedcba" 返回的是新的值</span></div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">//"abcdefg" 当然了，原始的是不会变的</span></div></pre></td></tr></table></figure>
<h1 id="sort-1"><a href="#sort-1" class="headerlink" title="sort()"></a>sort()</h1><p>Array.sort()方法将数组中的元素排序并返回排序后的数组。</p>
<p>当不带参数时，默认按照顺序排序，也就是从小到大。当然，也可以直接给sort加一个比较函数比较</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">4</span>,<span class="number">7</span>];</div><div class="line">arr.sort();</div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//[1,4,7]</span></div><div class="line"> </div><div class="line">arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> a-b; <span class="comment">//从小到大</span></div><div class="line">&#125;);</div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//[1,4,7]</span></div><div class="line"> </div><div class="line">arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> b-a; <span class="comment">//从大到小</span></div><div class="line">&#125;);</div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//[7,4,1]</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> num = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">'one'</span>,<span class="string">'three'</span>,<span class="string">'Six'</span>,<span class="string">'Five'</span>);</div><div class="line">num.sort(); <span class="comment">//区分大小写排序</span></div><div class="line"><span class="built_in">console</span>.log(num); <span class="comment">// ["Five", "Six", "one", "three"]</span></div><div class="line">num.sort(<span class="function"><span class="keyword">function</span>(<span class="params">s,t</span>)</span>&#123;</div><div class="line"> <span class="keyword">var</span> a = s.toLowerCase();</div><div class="line"> <span class="keyword">var</span> b = t.toLowerCase();</div><div class="line"> <span class="keyword">if</span>(a&lt;b) <span class="keyword">return</span> <span class="number">-1</span>;</div><div class="line"> <span class="keyword">if</span>(a&gt;b) <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line"> <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;);</div><div class="line"><span class="built_in">console</span>.log(num); <span class="comment">// ["Five", "one", "Six", "three"]</span></div></pre></td></tr></table></figure>
<h1 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h1><p>Array.concat()方法创建并返回一个新数组，它的元素包括调用concat()的原始数组的元素和concat()的每个参数。</p>
<p>如果这些参数中的任何一个自身是数组，则连接的是数组的元素，而非数组本身。</p>
<p>但要注意，concat()不会递归扁平化数组的数组。concat()也不会修改调用的数组。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</div><div class="line"><span class="built_in">console</span>.log(arr.concat(<span class="number">4</span>,<span class="number">5</span>)); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(arr);<span class="comment">// [1, 2, 3]</span></div><div class="line"><span class="built_in">console</span>.log(arr.concat([<span class="number">4</span>,<span class="number">5</span>])); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(arr.concat([<span class="number">4</span>,<span class="number">5</span>],[<span class="number">6</span>,<span class="number">7</span>])); <span class="comment">// [1, 2, 3, 4, 5,6,7]</span></div><div class="line"><span class="built_in">console</span>.log(arr.concat([<span class="number">4</span>,[<span class="number">5</span>,[<span class="number">6</span>,<span class="number">7</span>]]])); <span class="comment">// [1, 2, 3, 4, [5, [6, 7]]]</span></div><div class="line"><span class="built_in">console</span>.log(arr.concat(<span class="number">4</span>,[<span class="number">5</span>,[<span class="number">6</span>,<span class="number">7</span>]])); <span class="comment">// [1, 2, 3, 4, 5,[6,7]]</span></div></pre></td></tr></table></figure>
<h1 id="splice-1"><a href="#splice-1" class="headerlink" title="splice()"></a>splice()</h1><p>Array.splice()方法是在数组中插入或者删除元素的通用方法。它会修改原始数组的值，并返回一个新的数组序列</p>
<p>splice()的第一个参数指定了插入或删除的起始位置，第二个参数指定了应该从数组中删除的元素的个数。第二个参数省略则默认删到末尾。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>];</div><div class="line"><span class="built_in">console</span>.log(arr.splice(<span class="number">4</span>)); <span class="comment">//[5, 6, 7, 8]</span></div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">// [1, 2, 3, 4]</span></div><div class="line"><span class="built_in">console</span>.log(arr.splice(<span class="number">1</span>,<span class="number">2</span>));<span class="comment">// [2, 3]</span></div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">// [1, 4]</span></div></pre></td></tr></table></figure>
<p>splice()的前两个参数指定了需要删除的数组元素。紧随其后的任意个数的参数指定了需要插入到数组中的元素，并从第一个参数代表的位置开始插入。</p>
<p>不同于上边的concat()，splice()是直接把数组插进去，比如下面的[1,2]</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="built_in">console</span>.log(arr.splice(<span class="number">2</span>,<span class="number">0</span>,<span class="string">'a'</span>,<span class="string">'b'</span>)); <span class="comment">// []</span></div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">// [1, 2, "a", "b", 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(arr.splice(<span class="number">2</span>,<span class="number">1</span>,[<span class="number">1</span>,<span class="number">2</span>],<span class="number">3</span>));<span class="comment">// ["a"]</span></div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">// [1, 2, [1, 2], 3, "b", 3, 4, 5]</span></div></pre></td></tr></table></figure>
<h1 id="ECMAScript5中的数组方法"><a href="#ECMAScript5中的数组方法" class="headerlink" title="ECMAScript5中的数组方法"></a>ECMAScript5中的数组方法</h1><p>这一类数组方法大多数有统一大致的规则。它们都不会修改原始数组。<br>大多数方法的第一个参数接收一个函数，并且对数组的每个元素（或一些元素）调用一次该函数。</p>
<p>如果是稀疏数组，对不存在的元素不调用传递的函数；<br>在大多数情况下，调用的这个函数一般使用三个参数：数组元素、元素的索引、数组本身。通常后两个参数也不需要填写进去。<br>除了这里第一个参数（函数）之外，还有第二个参数（它是可选的），如果第二个参数存在，则调用的函数将被看做是第二个参数的方法。也就是说，在调用函数时传递进去的第二个参数作为它的this关键字的值来使用。</p>
<h1 id="forEach"><a href="#forEach" class="headerlink" title="forEach()"></a>forEach()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> sum = <span class="number">0</span>;</div><div class="line">data.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123; <span class="comment">//只使用了第一个参数（函数），调用的函数也只使用了第一个参数数组元素</span></div><div class="line"> sum += value;</div><div class="line">&#125;);</div><div class="line"><span class="built_in">console</span>.log(sum);<span class="comment">//15</span></div><div class="line"><span class="built_in">console</span>.log(data);<span class="comment">// [1, 2, 3, 4, 5]</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> sum = <span class="number">0</span>;</div><div class="line">data.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">value,item,data</span>)</span>&#123; <span class="comment">//调用的函数具有了三个参数</span></div><div class="line">  data[item] = value*value; <span class="comment">//取平方</span></div><div class="line">&#125;);</div><div class="line"><span class="built_in">console</span>.log(data);<span class="comment">// [1, 4, 9, 16, 25]</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.prototype.myEach = <span class="function"><span class="keyword">function</span>(<span class="params">fn</span>) </span>&#123;</div><div class="line">    <span class="comment">//调用myEach方法时传入fn参数就等于</span></div><div class="line">    <span class="comment">// var fn = function(item, i) &#123;</span></div><div class="line">    <span class="comment">//     console.log(item, i)</span></div><div class="line">    <span class="comment">// &#125;</span></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">         <span class="comment">//在循环里调用fn()函数,循环执行console语句</span></div><div class="line">         <span class="comment">//this[i],i都是实参</span></div><div class="line">         fn(<span class="keyword">this</span>[i], i)</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="map"><a href="#map" class="headerlink" title="map()"></a>map()</h1><p>这个方法将调用的数组中每个元素传递给指定的函数，并返回一个数组，它包含这个函数的返回值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> data1 = data.map(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> ++ value;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(data1);<span class="comment">// [2, 3, 4, 5, 6]</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.prototype.myMap = <span class="function"><span class="keyword">function</span>(<span class="params">fn</span>) </span>&#123;</div><div class="line">    <span class="keyword">var</span> tem = [];</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">        tem.push(fn(<span class="keyword">this</span>[i]))</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> tem</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="filter"><a href="#filter" class="headerlink" title="filter()"></a>filter()</h1><p>这个方法返回的数组元素是调用的数组的一个子集。传递的函数是用来逻辑判定的，该函数返回true或false。</p>
<p>如果返回值是true或者能转化为true的值，那么传递给判定函数的元素就是这个子集的成员，它将被添加到一个作为返回值的数组中。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> data1 = data.filter(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> value &lt;= <span class="number">3</span>;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="keyword">var</span> data2 = data.filter(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> value &gt; <span class="number">3</span>;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(data1);<span class="comment">// [1,2,3]</span></div><div class="line"><span class="built_in">console</span>.log(data2);<span class="comment">// [4,5]</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.prototype.myFilter = <span class="function"><span class="keyword">function</span>(<span class="params">fn</span>) </span>&#123;</div><div class="line">    <span class="keyword">var</span> tem = []</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (fn(<span class="keyword">this</span>[i])) &#123;</div><div class="line">            tem.push(<span class="keyword">this</span>[i])</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> tem</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="every-和some"><a href="#every-和some" class="headerlink" title="every()和some()"></a>every()和some()</h1><p>顾名思义，every()就是数组中所以元素都满足函数指定的条件时 返回true; some()就是某一项满足时就返回 true</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> data1 = data.every(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> value &lt; <span class="number">4</span>;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="keyword">var</span> data2 = data.some(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> value &gt;<span class="number">4</span>;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(data1);<span class="comment">// false</span></div><div class="line"><span class="built_in">console</span>.log(data2);<span class="comment">// true</span></div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.prototype.mySome = <span class="function"><span class="keyword">function</span>(<span class="params">fn</span>) </span>&#123;</div><div class="line"><span class="comment">// 返回值是：布尔值</span></div><div class="line"><span class="comment">// 如果找到第一个符合条件的元素，则直接返回 true ，同时终止遍历</span></div><div class="line"><span class="comment">// 如果遍历结果都没有符合条件的元素，则返回 false</span></div><div class="line">    <span class="keyword">var</span> isHave = <span class="literal">false</span></div><div class="line"><span class="comment">// var fn = function (item) &#123; return item % 2 === 0 &#125;</span></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (fn(<span class="keyword">this</span>[i])) &#123;</div><div class="line">             isHave = <span class="literal">true</span></div><div class="line">             <span class="keyword">break</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> isHave</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Array</span>.prototype.myEvery = <span class="function"><span class="keyword">function</span>(<span class="params">fn</span>) </span>&#123;</div><div class="line"><span class="comment">// 返回值是：布尔值</span></div><div class="line">    <span class="keyword">var</span> isEvery = <span class="literal">true</span></div><div class="line">    <span class="comment">// var fn = function (item) &#123; return item % 2 === 0 &#125;</span></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">        <span class="keyword">if</span> (!fn(<span class="keyword">this</span>[i])) &#123;</div><div class="line">            isEvery = <span class="literal">false</span></div><div class="line">            <span class="keyword">break</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"><span class="keyword">return</span> isEvery</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="reduce-和reduceRight"><a href="#reduce-和reduceRight" class="headerlink" title="reduce()和reduceRight()"></a>reduce()和reduceRight()</h1><p>这两个方法使用指定的函数将数组元素进行组合，生成单个值。</p>
<p>reduce()有两个参数。第一个是执行化简操作的函数，就是说用某种方法把两个值化简为一个值，并返回化简后的值。第二个参数可选，用来传递给第一个参数函数作为初始值。如果第二个参数没有，则初始值就使用数组的第一个元素值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"><span class="keyword">var</span> sum = data.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> a+b;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="keyword">var</span> sum1 = data.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> a+b;</div><div class="line">&#125;,<span class="number">5</span>);</div><div class="line"> </div><div class="line"><span class="keyword">var</span> min = data.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> (a&lt;b)?a:b;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data); <span class="comment">// [1, 2, 3, 4, 5]</span></div><div class="line"><span class="built_in">console</span>.log(sum);<span class="comment">// 15</span></div><div class="line"><span class="built_in">console</span>.log(sum1);<span class="comment">// 20</span></div><div class="line"><span class="built_in">console</span>.log(min);<span class="comment">// 1</span></div></pre></td></tr></table></figure>
<p>sum中没有第二个参数，所以初始值为第一个数组元素，第一步1+2=3，第二步3+3=6… 最后得15<br>sum1中有第二个参数，所以初始值为5，第一步5+1=6，第二步6+2=8… 最后得20</p>
<p>reduceRight()和reduce()差不多，不同的是它按照数组索引从高到低（从右到左）处理数组，而不是正常的从低到高。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]; </div><div class="line"><span class="keyword">var</span> str = data.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">x,y</span>)</span>&#123; <span class="comment">//顺序</span></div><div class="line"> <span class="keyword">return</span> x+y;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="keyword">var</span> str1 = data.reduceRight(<span class="function"><span class="keyword">function</span>(<span class="params">x,y</span>)</span>&#123; <span class="comment">//逆序</span></div><div class="line"> <span class="keyword">return</span> x+y;</div><div class="line">&#125;);</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data);<span class="comment">// [1, 2, 3]</span></div><div class="line"><span class="built_in">console</span>.log(str);<span class="comment">//"abc"</span></div><div class="line"><span class="built_in">console</span>.log(str1);<span class="comment">//"cba"</span></div></pre></td></tr></table></figure>
<h1 id="indexOf-和lastIndexOf"><a href="#indexOf-和lastIndexOf" class="headerlink" title="indexOf()和lastIndexOf()"></a>indexOf()和lastIndexOf()</h1><p>这个方法搜索整个数组中具有给定值的元素，返回找到的元素的索引（找到了一个就退出了），没有找到则返回-1.一个从头至尾，一个从尾至头</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> data = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>]; </div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data.indexOf(<span class="string">'a'</span>)); <span class="comment">//0</span></div><div class="line"><span class="built_in">console</span>.log(data.indexOf(<span class="string">'d'</span>)); <span class="comment">//-1</span></div><div class="line"><span class="built_in">console</span>.log(data.lastIndexOf(<span class="string">'a'</span>));<span class="comment">//4</span></div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(data.lastIndexOf(<span class="string">'a'</span>,<span class="number">-2</span>));<span class="comment">//2 从倒数第二个开始</span></div><div class="line"><span class="built_in">console</span>.log(data.lastIndexOf(<span class="string">'a'</span>,<span class="number">1</span>));<span class="comment">//0  从顺序第二个往前</span></div></pre></td></tr></table></figure>
<h1 id="数组类型-isArray"><a href="#数组类型-isArray" class="headerlink" title="数组类型 isArray()"></a>数组类型 isArray()</h1><p>判断一个对象是不是数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.isArray([]));<span class="comment">//true</span></div><div class="line"><span class="built_in">console</span>.log(<span class="built_in">Array</span>.isArray(&#123;&#125;));<span class="comment">//false</span></div><div class="line"> </div><div class="line"><span class="comment">//模拟上边的</span></div><div class="line"><span class="keyword">var</span> isArray1 = <span class="built_in">Function</span>.isArray||<span class="function"><span class="keyword">function</span>(<span class="params">o</span>)</span>&#123;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">typeof</span> o === <span class="string">"object"</span> &amp;&amp;</div><div class="line">  <span class="built_in">Object</span>.prototype.toString.call(o) === <span class="string">"[object Array]"</span>;</div><div class="line">&#125;;</div><div class="line"> </div><div class="line"><span class="built_in">console</span>.log(isArray1([]));<span class="comment">//true</span></div><div class="line"><span class="built_in">console</span>.log(isArray1(&#123;&#125;));<span class="comment">//false</span></div></pre></td></tr></table></figure>
<h1 id="reduce"><a href="#reduce" class="headerlink" title="reduce()"></a>reduce()</h1><p>对数组中的值累加减乘除</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</div><div class="line">      <span class="built_in">Array</span>.prototype.myReduce = <span class="function"><span class="keyword">function</span>(<span class="params">fn, initialValue</span>) </span>&#123;</div><div class="line">          <span class="keyword">if</span> (<span class="keyword">this</span>.length === <span class="number">0</span>) &#123;</div><div class="line">              <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'数组不能为空'</span>);</div><div class="line">          &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.length === <span class="number">1</span>) &#123;</div><div class="line">              <span class="keyword">return</span> <span class="keyword">this</span>[<span class="number">0</span>]</div><div class="line">          &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.length === <span class="number">2</span>) &#123;</div><div class="line">              <span class="keyword">return</span> fn(<span class="keyword">this</span>[<span class="number">0</span>], <span class="keyword">this</span>[<span class="number">1</span>])</div><div class="line">          &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.length &gt; <span class="number">2</span>) &#123;</div><div class="line">              <span class="keyword">if</span> (initialValue === <span class="literal">undefined</span>) &#123;</div><div class="line">                  <span class="keyword">var</span> ret = fn(<span class="keyword">this</span>[<span class="number">0</span>], <span class="keyword">this</span>[<span class="number">1</span>])</div><div class="line">                  <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">2</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">                      ret = fn(ret, <span class="keyword">this</span>[i])</div><div class="line">                  &#125;</div><div class="line">                  <span class="keyword">return</span> ret</div><div class="line">              &#125; <span class="keyword">else</span> &#123;</div><div class="line">                  <span class="keyword">var</span> ret = initialValue</div><div class="line">                  <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i++) &#123;</div><div class="line">                      ret = fn(ret, <span class="keyword">this</span>[i])</div><div class="line">                  &#125;</div><div class="line">                  <span class="keyword">return</span> ret</div><div class="line">              &#125;</div><div class="line">          &#125;</div><div class="line">      &#125;</div><div class="line"></div><div class="line">      <span class="keyword">var</span> ret = arr.myReduce(<span class="function"><span class="keyword">function</span>(<span class="params">prev, curr</span>) </span>&#123;</div><div class="line">          <span class="built_in">console</span>.log(prev, curr);</div><div class="line">          <span class="keyword">return</span> prev + curr</div><div class="line">      &#125;)</div><div class="line">      <span class="built_in">console</span>.log(ret);</div><div class="line"></div><div class="line">      <span class="keyword">var</span> users = [&#123;</div><div class="line">          age: <span class="number">18</span></div><div class="line">      &#125;, &#123;</div><div class="line">          age: <span class="number">16</span></div><div class="line">      &#125;, &#123;</div><div class="line">          age: <span class="number">3</span></div><div class="line">      &#125;]</div><div class="line"></div><div class="line">      <span class="keyword">var</span> ret1 = users.myReduce(<span class="function"><span class="keyword">function</span>(<span class="params">prev, curr</span>) </span>&#123;</div><div class="line">          <span class="built_in">console</span>.log(prev, curr);</div><div class="line">          <span class="keyword">return</span> prev + curr.age</div><div class="line">      &#125;, <span class="number">0</span>)</div><div class="line">      <span class="built_in">console</span>.log(ret1);</div></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/javascript/" rel="tag"># javascript</a>
          
            <a href="/tags/Array/" rel="tag"># Array</a>
          
            <a href="/tags/数组/" rel="tag"># 数组</a>
          
            <a href="/tags/prototype/" rel="tag"># prototype</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/12/18/JS字符串常用方法/" rel="next" title="JS字符串的常用方法">
                <i class="fa fa-chevron-left"></i> JS字符串的常用方法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/03/22/call方法-和-apply方法-以及-bind方法/" rel="prev" title="call方法和apply方法以及bind方法">
                call方法和apply方法以及bind方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://dynamic-image.yesky.com/300x-/uploadImages/upload/20140912/upload/201409/wylnwjynkxijpg.jpg"
               alt="shine-wl" />
          <p class="site-author-name" itemprop="name">shine-wl</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives/">
            
                <span class="site-state-item-count">21</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#对数组产生影响方法"><span class="nav-number">1.</span> <span class="nav-text">对数组产生影响方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#栈方法"><span class="nav-number">1.1.</span> <span class="nav-text">栈方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#push"><span class="nav-number">1.1.1.</span> <span class="nav-text">push</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#pop"><span class="nav-number">1.1.2.</span> <span class="nav-text">pop</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列方法"><span class="nav-number">1.2.</span> <span class="nav-text">队列方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#shift"><span class="nav-number">1.2.1.</span> <span class="nav-text">shift</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#unshift"><span class="nav-number">1.2.2.</span> <span class="nav-text">unshift</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重排序方法"><span class="nav-number">1.3.</span> <span class="nav-text">重排序方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#reverse"><span class="nav-number">1.3.1.</span> <span class="nav-text">reverse</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sort"><span class="nav-number">1.3.2.</span> <span class="nav-text">sort</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#操作方法"><span class="nav-number">1.4.</span> <span class="nav-text">操作方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#splice"><span class="nav-number">1.4.1.</span> <span class="nav-text">splice</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#所有方法总结"><span class="nav-number"></span> <span class="nav-text">所有方法总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#push-pop-unshift-shift"><span class="nav-number"></span> <span class="nav-text">push() pop() unshift() shift()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#join"><span class="nav-number"></span> <span class="nav-text">join()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#reverse-1"><span class="nav-number"></span> <span class="nav-text">reverse()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#sort-1"><span class="nav-number"></span> <span class="nav-text">sort()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#concat"><span class="nav-number"></span> <span class="nav-text">concat()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#splice-1"><span class="nav-number"></span> <span class="nav-text">splice()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ECMAScript5中的数组方法"><span class="nav-number"></span> <span class="nav-text">ECMAScript5中的数组方法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#forEach"><span class="nav-number"></span> <span class="nav-text">forEach()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#map"><span class="nav-number"></span> <span class="nav-text">map()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#filter"><span class="nav-number"></span> <span class="nav-text">filter()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#every-和some"><span class="nav-number"></span> <span class="nav-text">every()和some()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#reduce-和reduceRight"><span class="nav-number"></span> <span class="nav-text">reduce()和reduceRight()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#indexOf-和lastIndexOf"><span class="nav-number"></span> <span class="nav-text">indexOf()和lastIndexOf()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数组类型-isArray"><span class="nav-number"></span> <span class="nav-text">数组类型 isArray()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#reduce"><span class="nav-number"></span> <span class="nav-text">reduce()</span></a></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shine-wl</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动</div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">主题 &mdash; <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.2</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  

</body>
</html>
